import { reactive } from 'vue'
import { getLableData } from '@/utils/articleHttp'
import { baseURL } from '@/utils/interceptor'
export default {
  state: reactive({
    BaseUrl: baseURL,
    globalAvatarUrl: localStorage.getItem('globalAvatarUrl') || 'images/3.jpg',
    globalUsername: localStorage.getItem('globalUsername') || '未获取到',
    // 图书标签的数据
    lableData: [] as object[],
  }),
  // 修改username
  ChangeUsername(value: string) {
    this.state.globalUsername = value
    localStorage.setItem('globalUsername', value)
  },
  // 修改头像
  ChangeAvatar(value: string) {
    this.state.globalAvatarUrl = value
    localStorage.setItem('globalAvatarUrl', value)
  },
  // 获取图书标签的数据
  GetLableData() {
    if (this.state.lableData.length) return
    getLableData()
      .then((res) => {
        this.state.lableData = res.data.info
      })
      .catch((err) => err)
  },
  // 往图书标签里的数组加数据
  PushLableData(value: object) {
    this.state.lableData.push({ ...value, articleCount: '0' })
  },
  // 删除图书标签中的数据
  deleteLableData(index: number) {
    this.state.lableData.splice(index, 1)
  },
  // 添加图书给对应的标签加上一个1
  addLableArticle(label: string) {
    this.state.lableData.forEach((item) => {
      ;(item as any).labelName == label &&
        ((item as any).articleCount = Number((item as any).articleCount) + 1)
    })
  },
  // 删除图书给对应的标签减上一个1
  subtractLableArticle(label: string) {
    this.state.lableData.forEach((item) => {
      ;(item as any).labelName == label &&
        ((item as any).articleCount = Number((item as any).articleCount) - 1)
    })
  },
}
